#include "Aip1632.h"

void AiP1632C_init()//初始化
{    
	
	MR88FX02_Gpio_Set_Mode(AiP1632C_CS,OUTPUT);
	MR88FX02_Gpio_Set_Mode(AiP1632C_DATA,OUTPUT);
	MR88FX02_Gpio_Set_Mode(AiP1632C_WR,OUTPUT);
	MR88FX02_Gpio_Set_Mode(AiP1632C_RD,OUTPUT);
	Wait_ms(1000);
	MR88FX02_Gpio_Set(AiP1632C_RD, 1);
//	P1IO = 0x00;
	for(int i=0;i<10;i++)
	{
	AiP1632C_write_cmd(0x01);//打开系统振荡（关闭0x00，打开0x01)
	AiP1632C_write_cmd(0x08);//关闭闪烁功能（关闭0x08，打开0x09）
	AiP1632C_write_cmd(0x18);//RC主机模式（关闭0x10，打开0x18）
  AiP1632C_write_cmd(0xAF);//占空比16/16
	AiP1632C_write_cmd(0x03);//打开占空比发生器(关闭0x02，打开0x03）
//	AiP1632C_write_cmd(0xa1);//占空比2/16
	AiP1632C_write_cmd(0x24);//16COM NMOS开漏
		Wait_ms(20);
		AiP1632C_write(0x0,19);//更新显示
		Wait_ms(20);                                                         
		AiP1632C_write(0x0,19);//更新显示
	}
}


void AiP1632C_write_byteH(unsigned char byte,unsigned char length)//高位先传
{
	unsigned char i;
//	P2IO = 0xff;
	MR88FX02_Gpio_Set(AiP1632C_WR, 1);
	delay();
	for(i=0;i<length;i++)
	{
		MR88FX02_Gpio_Set(AiP1632C_WR,0);
		delay();
		MR88FX02_Gpio_Set(AiP1632C_DATA,(byte&0x80)>>7);//高位先传
		delay();
		MR88FX02_Gpio_Set(AiP1632C_WR , 1);//上升沿锁存
		delay();
		byte = byte<<1;
	}
	delay();
}

void AiP1632C_write_byteL(uint16_t byte,unsigned char length)//低位先传
{
	unsigned char i;
//	P2IO = 0xff;
	MR88FX02_Gpio_Set(AiP1632C_WR , 1);
	delay();
	for(i=0;i<length;i++)
	{
		MR88FX02_Gpio_Set(AiP1632C_WR , 0);
		delay();
		MR88FX02_Gpio_Set(AiP1632C_DATA , byte & 0x01);//低位先传
		delay();
		MR88FX02_Gpio_Set(AiP1632C_WR , 1);//上升沿锁存
		delay();
		byte >>= 1;
	}
	delay();
}

void AiP1632C_write_cmd(unsigned char cmd) //写命令
{
	MR88FX02_Gpio_Set(AiP1632C_CS , 1);
	delay();
	MR88FX02_Gpio_Set(AiP1632C_CS , 0);
	delay();
	AiP1632C_write_byteH(0x80,3);//传命令码100
	AiP1632C_write_byteH(cmd,9);//传9位指令
}

void AiP1632C_write_dat(unsigned char dat,unsigned char addr)//写数据
{
	unsigned char i;
	MR88FX02_Gpio_Set(AiP1632C_CS , 1);
	delay();
	MR88FX02_Gpio_Set(AiP1632C_CS , 0);
	delay();
	AiP1632C_write_byteH(0xa0,3);//传命令码101
	AiP1632C_write_byteL(addr,7);//传地址码，起始地址00H
	for(i=0;i<76;i++)
	{
		AiP1632C_write_byteL(dat,4);//传4位数据,低位先传
	}
	delay();
	delay();
	delay();
}


void AiP1632C_write(unsigned char addr,unsigned char send_times)//写数据
{
	uint16_t led_data = 0;
	MR88FX02_Gpio_Set(AiP1632C_CS , 1);
	delay();
	MR88FX02_Gpio_Set(AiP1632C_CS , 0);
	delay();
	AiP1632C_write_byteH(0xa0,3);//传命令码101
	AiP1632C_write_byteH(addr<<1,7);//传地址码，起始地址00H
	for(int i=(addr/4);i<(send_times+(addr/4));i++)
	{
		for(int j=0;j<16;j++)
		{
			led_data |= (LED[i][j]<<j);
		}

		AiP1632C_write_byteL(led_data,16);
		led_data = 0;
	}
	delay();

}








unsigned int value;
//======================================================
// interrupt routines
//======================================================



//======================================================
// peripheral setting routines
//======================================================


void delay(void)
{
	unsigned char i;
	for(i=0;i<10;i++);
}


void Wait_ms(int num) 
{
  	int i,j;

	for(i=0;i<num;i++)
	{
  		for(j=0;j<2286;j++);
	}
}


